package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.mapper.DictMapper;
import com.xmy.cultivate.service.IDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author min
 * @since 2022-07-26
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Autowired
    DictMapper dictMapper;
    public Integer getMaxNumForType(Integer type){
        return dictMapper.getMaxNumForType(type);
    }

    public IPage<Dict> findAll(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper){
        return dictMapper.findAll(page,wrapper);
    }

    public List<Map<String,Object>> getList(@Param(Constants.WRAPPER) Wrapper wrapper){
        return dictMapper.getList(wrapper);
    }

    public Dict getDictByNumAndType(Integer num,Integer type){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("num",num);
        dictQueryWrapper.eq("type",type);
        dictQueryWrapper.last("limit 1");
        return dictMapper.selectOne(dictQueryWrapper);
    }

    @Override
    public String getDictNameByNumAndType(Integer num,Integer type){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("num",num);
        dictQueryWrapper.eq("type",type);
        dictQueryWrapper.last("limit 1");
        Dict dict = dictMapper.selectOne(dictQueryWrapper);
        String dictName = "";
        if (dict != null){
            dictName = dict.getName();
        }
        return dictName;
    }


    /**
     * 获取学期
     * @param num
     * @return
     */
    public Dict getQuarter(Integer num){
        Integer type = 19;
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("num",num);
        dictQueryWrapper.eq("type",type);
        dictQueryWrapper.last("limit 1");
        return dictMapper.selectOne(dictQueryWrapper);
    }

    /**
     * 获取学期
     * @param num
     * @return
     */
    public Dict getNextQuarter(Integer num){
        Integer type = 19;
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("num",num);
        dictQueryWrapper.eq("type",type);
        dictQueryWrapper.last("limit 1");
        Dict dict = dictMapper.selectOne(dictQueryWrapper);
        if (dict !=null){
            dict = dictMapper.selectOne(new QueryWrapper<Dict>().eq("num",dict.getNum()).eq("type",type).last("limit 1"));
        }else {
            dict =null;
        }
        return dict;
    }

    /**
     * 获取是否假期
     * @param num
     * @return
     */
    public Integer getIsVacation(Integer num){
        Dict dict = this.getQuarter(num);
        return dict.getTag();
    }

    @Override
    public String getDictNameForNum(Integer num, Integer type){
        return  dictMapper.getDictNameForNum(num,type);
    }
}
